import pygame
from pygame.sprite import Group

from settings import Settings
from ship import Ship
from game_stats import GameStats
import game_functions as gf
from button import Button
from scoreboard import Scoreboard


def run_game():
    # 初始化游戏pygame
    pygame.init()
    # 游戏设置
    ai_settings = Settings()
    # 创建一个屏幕对象
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')
    # 创建飞船
    ship = Ship(ai_settings, screen)
    # 创建一个存储子弹的编组
    bullets = Group()
    # 创建一个外星人,外星人编组
    aliens = Group()
    gf.create_fleet(ai_settings, screen, ship, aliens)
    # 创建一个游戏统计信息实例, 得分牌
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)
    # 创建play按钮
    play_button = Button(ai_settings, screen, "Play")

    # 开始游戏的主循环
    while True:
        # 监视键盘和鼠标事件
        gf.check_events(sb, ai_settings, screen, stats, play_button, ship, aliens, bullets)

        if stats.game_active:
            # 更新飞船位置
            ship.update()
            # 更新子弹
            gf.update_bullets(ai_settings, screen, ship, aliens, bullets, stats, sb)
            # 更新外星人
            gf.update_aliens(sb, ai_settings, stats, screen, ship, aliens, bullets)

        # 刷新屏幕
        gf.update_screen(sb, ai_settings, stats, screen, ship, aliens, bullets, play_button)


run_game()
